package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.game.buff.Buff;
import com.xcity.game.build.BuildLevelUpTemplate;
import com.xcity.game.build.BuildTemplate;
import com.xcity.game.build.BuildUtils;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.common.ConfirmOperation;
import com.xcity.game.formation.Formation;
import com.xcity.game.pet.Pet;
import com.xcity.game.player.PlayerUtils;
import com.xcity.game.player.template.PlayerLevelTemplate;
import com.xcity.game.rank.RankingList;
import com.xcity.game.rank.RankingListEnum;
import com.xcity.game.relation.RelationService;
import com.xcity.game.skill.PlayerSkill;
import com.xcity.game.worldevent.WorldEventService;
import com.xcity.pomelo.push.msg.BuffsPush.BuffInfo;
import com.xcity.pomelo.response.BuildStatusResponse.AttackerInfo;
import com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo;

import naga.x.App;
import naga.x.game.time.TimeUpdater;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;
import naga.x.util.CommonUtils;

@Deprecated
@PomeloMessage(type = Message.Type.RESPONSE)
public class PlayerInfoResponse extends Response {
	
	private Role roler;
	
	public static class Role {
		public long exp;
		public int curExp;
		public int nextExp;
		public int curOpenBuild;
		public int nextOpenBuild;
		public int nextOpenBuildNeedLv;
		public long gold;
		public int health;
		public long id;
		public String img;
		public int lv;
		public String sign;
		public long roleId;
		public String roleName;
		public byte gender;
		public int stone;
		public long totalGold;
		public int vipLv;
		public byte robStatus;
		public int robLostGold;
		public int robLostStone;
		public byte isProtect;
		public int bsRemainingTime;
		public int buildSpeed;
		public int ipRemainingTime;
		public List<Build> curBuilds;
		public List<BuffInfo> buffs;
		public String dog; // 看门狗
//		public int IQ, EQ, AQ, 
		public int leadership;
		public int intimacy;
		public int charm;
		@Deprecated
		public int attack;
		public int hp;
		public int strength;
		public int maxStr;
		public int rank;
		public int combatPower;
		public CombatAttributeResponse attr;
		public PlayerSkill skill;
		public List<EmployeeSimpleInfo> formation = Collections.emptyList(); // 战斗阵型
		public byte isFriend; // 与自己是否为好友（1：是，0：否）
	}
	
	public static class Build {
		public int buildId;
		public int health;
		public int id;
		public String img;
		public int income;
		public int incomeAdded; // 每分钟加成收入(可为负，受世界事件影响)
		public int level;
		public int maxIncome;
		public int money;
		public String name;
		public int price;
		public int remainingTime;
		public int cdTime;
		public byte status; // 0:建造/升级中 ; 1:空闲状态; 2:休店期
		public int restCD; // 休店期剩余cd
		public AttackerInfo attacker = AttackerInfo.EMPTY;
		public int slot;
		public int maxSlot;
		public float defense;
		public List<EmployeeSimpleInfo> employees = Collections.emptyList();
		public int decEmployeeHP; // 被攻击时消耗对方员工生命力
		public byte spy; // 是否窥探过
	}
	
	@SuppressWarnings("unused")
	public PlayerInfoResponse(int serial, Player player, Player actor) {
		super(serial);
		roler = new Role();
		roler.exp = actor.getExp();
//		setLevel(PlayerUtils.exp2Lvl(exp));
		roler.curExp = (int) actor.getExp();//PlayerUtils.getExpOfNowLvl(actor.getExp());
		roler.nextExp = PlayerUtils.getExpCalc().getExp(actor.getLevel() + 1);//PlayerUtils.getNextLvlNeedExp(actor.getLevel());
		roler.curOpenBuild = PlayerUtils.getMaxBuildNum(actor.getLevel());
		roler.gold = actor.getGold();
		roler.health = 0;//PlayerUtils.calcRisk(actor.getBuildings().getBuildings());
		roler.id = actor.getId();
		roler.img = actor.getIcon();
		roler.lv = actor.getLevel();
		roler.vipLv = actor.getVipLvl();
		roler.roleId = actor.getId();
		roler.roleName = actor.getName();
		roler.gender = actor.getSex();
		roler.stone = actor.getStone();
		roler.totalGold = actor.getFixedAssets();//PlayerUtils.calcFixedAssets(actor.getId(), actor.getBuildings().getBuildings());
//		private List<Build> curBuilds;
		List<com.xcity.db.entity.Build> srcs = null;//actor.getBuildings().getBuildings();
		if (srcs == null || srcs.isEmpty()) {
			roler.curBuilds = Collections.emptyList();
			roler.buffs = Collections.emptyList();
		} else {
			roler.curBuilds = new ArrayList<Build>(srcs.size());
			for (int i = 0; i < srcs.size(); i++) {
				com.xcity.db.entity.Build src = srcs.get(i);
				src.update(); // 强制更新一下状态
				BuildLevelUpTemplate lvlTemp = src.getBuildLevelUpTemplate();
				BuildTemplate temp = src.getTemplate();
				Build b = new Build();
				roler.curBuilds.add(b);
				b.buildId = src.getId();
				b.id = src.getId();
				b.level = src.getLevel();
				b.img = temp.getImage(src.getLevel());
				b.money = src.settle();
				b.name = temp.getName(src.getLevel());
				b.remainingTime = src.getLeftCD();
				b.cdTime = lvlTemp.getCd() * 1000;
				b.status = src.getState().codeOfOldVersion();
				b.restCD = src.getLeftRestCD();
				b.slot = src.getSlot();
				b.maxSlot = com.xcity.db.entity.Build.MAX_SLOT;
				b.defense = src.getDefense();
				b.decEmployeeHP = src.getBuildLevelUpTemplate().getDecEmployeeHP();
				Player owner = src.owner(); // owner re get
				if (src.getAttackerId() != 0L) {
					b.attacker = new AttackerInfo(owner.getId() == src.getAttackerId() ? owner : src.attacker());
				}
				
				if (owner.getId() != player.getId() && b.level > com.xcity.db.entity.Build.BREAK_LEVEL && BuildUtils.getLastSpyDay(player, actor, src) != TimeUpdater.getInstance().today()) {
					// 没有侦查该建筑
					continue;
				}
				b.spy = ConfirmOperation.YES;
				// 需要侦查的属性 SPY
				b.health = lvlTemp.getRisk();
				int incomePerMinute = src.getIncomePerMinute();
				// SPY
				b.income = /*lvlTemp.getIncome()*/incomePerMinute;
				// SPY
				b.incomeAdded = (int) (/*lvlTemp.getIncome()*/incomePerMinute * src.getFactor() - /*lvlTemp.getIncome()*/incomePerMinute);
				// SPY
				b.maxIncome = /*lvlTemp.getMaxIncome()*/src.getMaxIncome();
				// SPY
				b.price = lvlTemp.getPrice();
				// SPY
				long[] employeeIds = src.employees();
				if (employeeIds != null) {
					b.employees = new ArrayList<EmployeeSimpleInfo>(employeeIds.length);
					for (long id : employeeIds) {
						Employee e;
						if (id != 0 && (e = owner.getEmployees().get(id)) != null) {
							b.employees.add(new EmployeeSimpleInfo(e));
						} else {
							b.employees.add(EmployeeSimpleInfo.EMPTY);
						}
					}
				}
			}
			if (actor.getBuffs() != null && actor.getBuffs().getBuffs().size() > 0) {
				List<Buff> buffs = actor.getBuffs().getBuffs();
				roler.buffs = new ArrayList<BuffInfo>(buffs.size());
				for (int j = 0, n = buffs.size(); j < n; j++) {
					Buff buff = buffs.get(j);
					if (buff.getId() > WorldEventService.buffIdOffset) {
						BuffInfo bi = new BuffInfo(buff);
						roler.buffs.add(bi);
					}
				}
			} else {
				roler.buffs = Collections.emptyList();
			}
		}
		Pet pet = actor.getPets() != null ? actor.getPets().getWorkingPet() : null;
		roler.dog = pet != null ? pet.getTemplate().getAnimation() : "";
		PlayerLevelTemplate playerTemplate = PlayerUtils.getTemplate(actor);
//		roler.IQ = playerTemplate.getIQ();
//		roler.EQ = playerTemplate.getEQ();
//		roler.AQ = playerTemplate.getAQ();
		roler.leadership = PlayerUtils.getLeadership(actor);
		roler.intimacy = 0;
		roler.charm = PlayerUtils.getCharm(actor);
		roler.attack = (int) actor.getAttackPower();//PlayerUtils.getAttack(actor);
		roler.hp = actor.getHp();//PlayerUtils.getHP(actor);
		roler.strength = actor.getStrength();
		roler.maxStr = PlayerUtils.getMaxStrength(actor);
//		Session sess = actor.getSession();
		RankingList rl = RankingListEnum.ASSETS.instance;
		Ranking rank = rl.findEntry(actor);
		roler.rank = rank != null ? rank.getIndex() + 1 : rl.getCapacity() + CommonUtils.randomInt(10, 100);
		roler.combatPower = CombatUtils.getCombatPower(actor, actor.getFormations().get(Formation.TYPE_NORMAL).getUnits());
		roler.attr = new CombatAttributeResponse(actor);
		roler.skill = null;//actor.getSkillManager() != null ? actor.getSkillManager().getActivatedSkill() : null;
//		long[] eids = CombatUtils.getCombatFormation(actor);
		Employee[] emps = player.getFormations().get(Formation.TYPE_NORMAL).getUnits();
		if (emps != null && emps.length > 0) {
			roler.formation = new ArrayList<>(emps.length);
			for (Employee e : emps) {
				roler.formation.add(e != null ? new EmployeeSimpleInfo(e) : EmployeeSimpleInfo.EMPTY);
			}
		}
		roler.isFriend = (byte) (App.getApp().getServiceManager().get(RelationService.class).isFriend(player, actor) ? 1 : 0);
	}

	public Role getRoler() {
		return roler;
	}

	public void setRoler(Role roler) {
		this.roler = roler;
	}
	
}
